My name is Guangling and I make websites with interactive graphics !!!
library(tidyverse)
## -- Attaching packages ---------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.2.1 √ purrr 0.3.2
## √ tibble 2.1.3 √ dplyr 0.8.3
## √ tidyr 1.0.0 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.4.0
## -- Conflicts ------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(viridis)
## Loading required package: viridisLite
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
data("nyc_airbnb")
nyc_airbnb =
nyc_airbnb %>%
mutate(rating = review_scores_location / 2) %>%
select(
neighbourhood_group, neighbourhood, rating, price, room_type, lat, long) %>%
filter(
!is.na(rating),
neighbourhood_group == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500) %>%
sample_n(5000) ## give 5000 samples from the dataset
nyc_airbnb %>%
mutate(text_label = str_c("Price: $", price, '\nRating: ', rating)) %>%
plot_ly(
x = ~lat, y = ~long, type = "scatter", mode = "markers",
color = ~price, text = ~text_label, alpha = 0.5)
## mode = "markers" just put the points but not line the points up.
common_neighborhoods =
nyc_airbnb %>%
count(neighbourhood, sort = TRUE) %>%
top_n(8) %>%
select(neighbourhood)
## Selecting by n
inner_join(nyc_airbnb, common_neighborhoods, by = "neighbourhood") %>%
mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>% ## increasing order with prices
plot_ly(y = ~price, color = ~neighbourhood, type = "box",
colors = "Set2")
nyc_airbnb %>%
count(neighbourhood) %>%
mutate(
neighbourhood = fct_reorder(neighbourhood, n)
) %>%
plot_ly(x = ~neighbourhood, y = ~n , type = "bar")